package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.util.AppUtil;

/**
 * Created by Administrator on 2016/11/18.
 */

public class WebViewActivity extends BaseActivity {

    WebView webview;
    String url;
    String title;
    Handler timeHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initView();
        initData();
        startThread();
    }

    private void startThread() {
        AppUtil.showLoading(this);
        timeHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    AppUtil.closeLoading();
                }

            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    Message message = Message.obtain();
                    message.what = 0;
                    timeHandler.sendMessage(message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        webview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webview.getSettings();
        //webview.getSettings().setPluginsEnabled(true);
        // 将图片调整到适合webview的大小
        webSettings.setUseWideViewPort(true);
        // 支持内容重新布局
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 缩放至屏幕的大小
        webSettings.setLoadWithOverviewMode(true);
        // 设置支持Javascript的参数
        webSettings.setJavaScriptEnabled(true);
        // 支持通过JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);

        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        // 手动开启DOM Storage
        webSettings.setDomStorageEnabled(true);
        // 设置允许访问文件数据
        webSettings.setAllowFileAccess(true);
        // webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setDatabaseEnabled(true);

        findViewById(R.id.tv_left).setOnClickListener(this);


    }

    private void initData() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        ((TextView) findViewById(R.id.tv_title)).setText(title == null ? "" : title);
        if (url != null) {
            webview.loadUrl(url);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                final ViewGroup viewGroup = (ViewGroup) webview.getParent();
                if (viewGroup != null) {
                    viewGroup.removeView(webview);
                }
                webview.removeAllViews();
                webview.destroy();
                finish();
                break;
        }
    }
}
